package com.robot.agent.controller;

import com.alibaba.fastjson.JSON;
import com.robot.agent.pojo.Agent;
import com.robot.agent.service.AgentService;
import entity.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * agent控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/agent")
public class AgentJsonController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AgentService agentService;

    /**
     * 条件查询代理商
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/third/findAgentsBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindAgentsBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Agent> pageData = agentService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 通过ID查询代理商
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/third/findAgentById/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindAgentById(@PathVariable String id) {
        return JSON.toJSONString(agentService.findById(id));
    }

    /**
     * 通过phone查询代理商
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/third/findAgentByPhone/{phone}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindAgentByPhone(@PathVariable String phone) {
        return JSON.toJSONString(agentService.findAgentByPhone(phone));
    }

    /**
     * 通过areaId和status查询agent
     *
     * @param areaId
     * @param status
     * @return
     */
    @RequestMapping(value = "/third/findAgentByAreaIdAndStatusAndDeletedAtIsNull/{areaId}/{status}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindAgentByAreaIdAndStatusAndDeletedAtIsNull(@PathVariable String areaId, @PathVariable String status) {
        return JSON.toJSONString(agentService.findByAreaIdAndStatusAndDeletedAtIsNull(areaId, status));
    }

    /**
     * 新增代理商
     *
     * @param agent
     */
    @RequestMapping(value = "third/addAgent", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public void thirdAddAgent(@RequestBody String agent) {
        agentService.add(JSON.parseObject(agent, Agent.class));
    }

    /**
     * 修改代理商
     *
     * @param agent
     */
    @RequestMapping(value = "/third/updateAgent", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public void thirdUpdateAgent(@RequestBody String agent) {
        agentService.update(JSON.parseObject(agent, Agent.class));
    }

    /**
     * 硬删除代理商
     *
     * @param id
     */
    @RequestMapping(value = "/third/deleteAgentById/{id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public void thirdDeleteAgentById(@PathVariable String id) {
        agentService.deleteById(id);
    }
}
